package com.myapplication.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.myapplication.model.CartInfo;
import com.myapplication.model.Store;
import com.myapplication.zhou.alldemo.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhoushikun on 2016/7/12.
 */
public class ShopCartRecycleAdapter extends RecyclerView.Adapter<ShopCartRecycleAdapter.CartViewHolder> implements CartInfoRecycleAdapter.JiaJianClickListener,
        CartInfoRecycleAdapter.ItemClickListener {
    private List<Store> stores = new ArrayList<>();
    private Context context;
    private LayoutInflater inflater;
    private CheckClick checkClick;
    private RecyclerView.LayoutManager layoutManager;
    private CheckClickOnChild checkClickOnChild;
    private JiaJianClick jiaJianClick;

    public ShopCartRecycleAdapter(Context context, List<Store> stores) {
        this.stores = stores;
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public CartViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.shop_cart_recycle_view, parent, false);
        CartViewHolder cartViewHolder = new CartViewHolder(view, checkClick);
        return cartViewHolder;
    }

    @Override
    public void onBindViewHolder(CartViewHolder holder, int position) {
        holder.textView_store_name.setText(stores.get(position).getStore());
        holder.checkBox.setChecked(stores.get(position).getChecked());
        holder.bindView(stores.get(position));
        layoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        holder.recyclerView1.setLayoutManager(layoutManager);
        CartInfoRecycleAdapter cartInfoRecycleAdapter = new CartInfoRecycleAdapter(context, stores.get(position).getList());
        holder.recyclerView1.setAdapter(cartInfoRecycleAdapter);
        cartInfoRecycleAdapter.OnItemClickListener(this);
        cartInfoRecycleAdapter.OnJiaJianClickListener(this);
    }

    public void addOnItemCheckClick(CheckClick checkClick) {
        this.checkClick = checkClick;
    }

    public void addOnItemChildCheck(CheckClickOnChild checkClickonChild){
        this.checkClickOnChild = checkClickonChild;
    }

    public void jiajianClick(JiaJianClick jiajianClick){
        this.jiaJianClick = jiajianClick;
    }

    public void Update(List<Store> stores) {
        this.stores = stores;
        notifyDataSetChanged();

    }

    public void update(int position,CartViewHolder holder) {
        layoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
        holder.recyclerView1.setLayoutManager(layoutManager);
        CartInfoRecycleAdapter cartInfoRecycleAdapter = new CartInfoRecycleAdapter(context, stores.get(position).getList());
        holder.recyclerView1.setAdapter(cartInfoRecycleAdapter);
        notifyDataSetChanged();
        cartInfoRecycleAdapter.notifyDataSetChanged();
    }



    @Override
    public int getItemCount() {
        return stores.size();
    }

    @Override
    public void onItemClick(View view, CartInfo cartInfo, int position) {
        int j  = 0;
        for (;j<stores.size();j++) {
            if (stores.get(j).getStore().equals(cartInfo.getStore_name())) {
                break;
            }
        }
        checkClickOnChild.onChild(view, cartInfo, position, j);
    }

    /**
     * 数量添加监听
     */
    @Override
    public void onJiaJianClick(View view, CartInfo cartInfo, int postion) {
        int j  = 0;
        for (;j<stores.size();j++) {
            if (stores.get(j).getStore().equals(cartInfo.getStore_name())) {
                break;
            }
        }
        jiaJianClick.jiajian(view, cartInfo, postion,j);
    }

    public class CartViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private CheckBox checkBox;
        private TextView textView_store_name;
        private RecyclerView recyclerView1;
        private CheckClick checkClick;


        public CartViewHolder(View itemView, CheckClick checkClick) {
            super(itemView);
            this.checkClick = checkClick;
            checkBox = (CheckBox) itemView.findViewById(R.id.checkbox);
            textView_store_name = (TextView) itemView.findViewById(R.id.textView_store_name);
            recyclerView1 = (RecyclerView) itemView.findViewById(R.id.recycleView1);
            checkBox.setOnClickListener(this);
        }


        public void bindView(Store store) {
            checkBox.setTag(store);
        }

        @Override
        public void onClick(View v) {
            if(v.getId()==R.id.checkbox)
            checkClick.onCheckClick(v, (Store) v.getTag(), getLayoutPosition());
        }
    }

    public interface CheckClick {
        void onCheckClick(View v, Store store, int position);
    }

    public interface CheckClickOnChild {
        void onChild(View v, CartInfo cartInfo, int position, int listPosition);
    }

    public interface JiaJianClick {
        void jiajian(View v, CartInfo cartInfo, int position,int listPosition);
    }

}
